package uit.nfc.nfcmenudishes;

import uit.nfc.utils.DishesItem;
import uit.nfc.utils.GetAllDishesAsyncTask;
import uit.nfc.utils.NfcUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class NfcMenuActivity extends Activity {

	public final static String PREFIX = "NMD";
	
	private Tag tag;
	private Context context;
	private EditText editTable;
	private Spinner spinnerDishes;
	private EditText editId;
	private Button buttonWrite;
	
	private NdefMessage message;
	private String table;
	private String id;
	private DishesItem dishes;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nfc_menu);
		
		context = this;
		editTable = (EditText)findViewById(R.id.edit_table);
		spinnerDishes = (Spinner)findViewById(R.id.spinner_dishes);
		editId = (EditText)findViewById(R.id.edit_id);
		buttonWrite = (Button)findViewById(R.id.button_write);
		
		//new GetAllDishesAsyncTask(this).execute();
		
		buttonWrite.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				table = editTable.getText().toString();
				id = editId.getText().toString();
				
				if (table.isEmpty() || id.isEmpty()) {
					NfcUtils.toast("Please enter Table or select Dishes!", context);
				} else {
					//dishes = (DishesItem)spinnerDishes.getSelectedItem();
					
					NdefRecord[] records = new NdefRecord[3];
					records[0] = NfcUtils.createNdefTextRecord(PREFIX);
					records[1] = NfcUtils.createNdefTextRecord(table);
					records[2] = NfcUtils.createNdefTextRecord(id);
					message = new NdefMessage(records);
					
					NfcUtils.toast("Please touch tag to write!", v.getContext());
					NfcUtils.enableTagWriteMode((Activity)v.getContext());
				}
			}
		});
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
    	tag = NfcUtils.getTag(intent);
    	
    	boolean success = NfcUtils.writeTag(tag, message, context);
		if (success) {
			
			AlertDialog dialog = new AlertDialog.Builder(context).create();
			dialog.setTitle("Notice");
			dialog.setMessage("Table " + table + ".\nDishes id " + id + ".\nWriting on tag successful!");
			
			dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			
			dialog.show();
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		NfcUtils.disableTagWriteMode(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		NfcUtils.enableTagWriteMode(this);
	}

	public Spinner getSpinnerDishes() {
		return spinnerDishes;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_nfc_menu, menu);
		return true;
	}
}
